var app = getApp();
Page({
    data: {
        isIPX: app.globalData.isIPX,
        style: 0,
        curPage : '/zofui_sales/sort/index',
        page : {
            isend : false,
            doing : false,
            doo : 'sort_v1',
            pdata : {
                op : 'list',
                page : 1,
                sortid : 0, // 分类id
                sorttwo : 0,
                shopsorttwo : 0,
                otype : 0,
                gtype : 0,
                tag : 0,
            },
            waitf: 0,
            nodataf: 0,
        },
        datalist: [],
        sort: [],
        sortoneindex: 0,
        sorttwoindex: 0,
        color: '',
        citylist: [],
        tab0name: '',
        goodstype: [{
                name: '全部',
                id: 0
            },
            {
                name: '普通',
                id: 1
            },
            {
                name: '限抢',
                id: 2
            },
            {
                name: '拼单',
                id: 3
            },
            {
                name: '减价',
                id: 4
            },
            {
                name: '报名',
                id: 5
            },
            {
                name: '抽奖',
                id: 6
            },
            {
                name: '秒杀',
                id: 7
            },
        ],
        tablist: [{
                name: '商品分类',
                id: 4,
                icon: 'zan-icon-arrow',
                rotate: true
            },
            {
                name: '离我最近',
                id: 1,
                icon: 'zan-icon-arrow',
                rotate: true
            },
            {
                name: '全部类型',
                id: 2,
                icon: 'zan-icon-arrow',
                rotate: true
            },
            {
                name: '浏览模式',
                id: 3,
                icon: 'zan-icon-wap-nav',
                rotate: false
            },
        ],
        ordertype: [{
            name: '距离最近',
            id: 0
        }, {
            name: '销量最高',
            id: 1
        }, {
            name: '价格最低',
            id: 2
        }],
        Time: app.globalData.Time,
        siteroot: app.siteInfo.siteroot.replace('app/index.php', ''),
        uniacid: app.siteInfo.uniacid,
    },
    onLoad: function (options) {
        var self = this;

        var zfid = options.zfid ? options.zfid : '';
        var shopid = options.shopid ? options.shopid : 0;
        var shopsort = options.shopsort ? options.shopsort : 0;

        self.setData({
            'page.pdata.sortid': options.status ? options.status : 0,
            'page.pdata.sorttwo': options.sorttwo ? options.sorttwo : 0,
            'page.pdata.shopsort': options.shopsort ? options.shopsort : 0,
            'page.pdata.shopsorttwo': options.shopsorttwo ? options.shopsorttwo : 0,
            'page.pdata.tag': options.tag ? options.tag : 0,
            'page.pdata.shopid': shopid,
            'page.pdata.page': 1,
            'page.isend': false,
            'page.waitf': 0,
            'page.nodataf': 0,
            'page.pdata.isinit': 0,
            options: options
        });

        app.com.getLocation(self, function (loc) {
            self.setData({
                'page.pdata.lat': loc.latitude,
                'page.pdata.lng': loc.longitude,
            });
            app.com.auth(function (user) {
                app.com.getPage(self, self.data.page, 20, function (res) {
                    var data = res.data.data;
                    var color = app.com.mainColor(data.params.style);

                    // 初始化页面获取购物车信息
                    self.getCartShop()
                    self.setData({
                        sort: data.sort,
                        datalist: data.list,
                        Pagetheme: data.params.style,
                        sharedata: data.sharedata,
                        params: data.params,
                        tab0name: data.sname,
                        listtype: data.params.opail == 1 ? true : false,
                        inited: true,
                        'page.pdata.isinit': 1,
                        citylist: data.citylist ? data.citylist : [],
                        color: color,
                        style: data.style
                    });


                    wx.setNavigationBarColor({
                        frontColor: '#ffffff',
                        backgroundColor: color,
                    });
                    wx.setNavigationBarTitle({
                        title: res.data.data.params.title ? res.data.data.params.title : '',
                    });

                }, false, function (res) {
                    app.com.alert(res.data.message);
                });
                app.com.setBar(app, self, shopid);
            })
        });

    },
    // 获取购物车信息
    getCartShop() {
        console.log('---');
        var self = this
        var options = this.options
        var zfid = '';
        app.com.auth(function (user) {
            app.com.getLocation(self, function (loc) {
                var postdata = {
                    op: 'info',
                    lat: loc.latitude,
                    lng: loc.longitude,
                    zfid: zfid,
                    shopid: 0,
                }
                app.com.http('cart', 'POST', postdata, 0, false, '', '', function (res) {
                    if (!res.data.errno && res.data) {
                        self.setData({
                            'cart.goodlist': res.data.data.goodlist,
                        });
                    }
                });
            })
        });
    },
    countit() {
        var url = '/zofui_sales/cart/cart';
        wx.navigateTo({
            url: url
        })
    },
    onShareAppMessage: function () {
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl: self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if (this.data.isdown) return false;
        this.isdown = true;
        this.onLoad(this.data.options);
        wx.stopPullDownRefresh();
    },
    listtypefn(e) {
        this.setData({
            listtype: e.detail
        })
    },
    setList(e) {
        var self = this;

        var setdata = {
            datalist: [],
            'page.pdata.page': 1,
            'page.isend': false,
            'page.waitf': 0,
            'page.nodataf': 0
        };
        for (var t in e.detail) {
            setdata[t] = e.detail[t];
        }
        self.setData(setdata);

        app.com.getPage(self, self.data.page, 20, function (res) {
            self.setData({
                datalist: res.data.data.list,
            });
        });
    },
    onReachBottom: function () {
        var self = this;
        app.com.getPage(self, self.data.page, 20, function (res) {
            self.setData({
                datalist: self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    location: function (e) {
        app.com.location(e);
    },
    otherapp: function (e) {
        app.com.otherapp(e);
    },
    navigateto: function (e) {
        app.com.navigateto(e);
    },
    redirectto: function (e) {
        app.com.redirectto(e);
    },
    callphone: function (e) {
        app.com.callphone(e);
    },
    toweburl: function (e) {
        app.com.toweburl(e);
    },
    showimages: function (e) {
        app.com.showimages(e);
    },
    footNavigate(e) {
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
            url: url
        })
    },

})